package com.koron.css2.newProjectCenter.impl;

import cn.hutool.core.util.ObjectUtil;
import com.koron.css2.baseConfig.bean.BankBean;
import com.koron.css2.baseConfig.mapper.BankMapper;
import com.koron.css2.common.dto.DocDto;
import com.koron.css2.common.service.AbstractExportDoc;
import com.koron.css2.newProjectCenter.bean.PjCollection;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjCollectionQuery;
import com.koron.css2.newProjectCenter.bean.query.PjMeterQuery;
import com.koron.css2.newProjectCenter.dto.IdsDto;
import com.koron.css2.newProjectCenter.mapper.PjCollectionMapper;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.newProjectCenter.vo.PjChargeMoreVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 工程收款-多用户打印水表领用单
 */
@Slf4j
@Service
public class PjChargeDocMoreExport extends AbstractExportDoc {

    @Override
    public DocDto getDocText(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            IdsDto idsDto = JsonUtils.objectToPojo(req.getData(), IdsDto.class);
            List<String> ids = idsDto.getIds();
            if (ids.isEmpty()|| StringUtils.isBlank(idsDto.getPjCollectionId())) {
                throw new RuntimeException("缺少参数");
            }
            PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
            PjCollectionMapper pjCollectionMapper = factory.getMapper(PjCollectionMapper.class);
            List<PjMeter> pjMeterList = new ArrayList<>();
            ids.forEach(id -> {
                PjMeterQuery query = new PjMeterQuery();
                query.setId(id);
                PjMeter pjMeter = pjMeterMapper.getSingle(query);
                pjMeterList.add(pjMeter);
            });

            Map<String, Object> param = new HashMap<>();
            for (int i = 1; i <= 5; i++) {
                param.put("pjMeter" + i, new PjChargeMoreVO());
            }

            //获取工程信息
            PjCollectionQuery query=new PjCollectionQuery();
            query.setId(idsDto.getPjCollectionId());
            PjCollection collection = pjCollectionMapper.getSingle(query);
            if (ObjectUtil.isNull(collection)){
                throw new RuntimeException("参数错误:"+idsDto.getPjCollectionId());
            }
            //设置打印参数
            param.put("projectCode", collection.getProjectCode());
            List<PjChargeMoreVO> voList = new ArrayList<>();
            setPrintVo(pjMeterList, voList, factory, param);
            param.put("meterList", voList);
            param.put("operator", userInfo.getUserInfo().getName());
            LocalDate billingDate = LocalDate.now();
            param.put("year1", billingDate.getYear());
            param.put("month1", billingDate.getMonthValue());
            param.put("day1", billingDate.getDayOfMonth());


            DocDto docDto = new DocDto();
            docDto.setTitle("多用户水表领用、安装签订单");
            docDto.setTemplate("报装水表多户领用单-死类型.docx");
            docDto.setParam(param);
            return docDto;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException("获取水表内容失败。");
        }
    }

    private void setPrintVo(List<PjMeter> pjMeterList, List<PjChargeMoreVO> voList, SessionFactory factory, Map<String, Object> param) {
        int index = 1;
        //设置循环实体的属性
        List<PjChargeMoreVO> pjChargeMoreVOList = new ArrayList<>();
        try {
            PjMeterList.covertMeterInfo(factory, pjMeterList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        for (PjMeter pjMeter : pjMeterList) {
            PjChargeMoreVO vo = new PjChargeMoreVO();
            vo.setNumber(index);
            vo.setCtmName(pjMeter.getCtmInfo().getCtmName());
            vo.setMobile(pjMeter.getUserInfo().getMobile());
            vo.setLastReadingNum(pjMeter.getUserInfo().getLastReadingNum());
            vo.setMeterBoreName(pjMeter.getMsInfo().getMeterBoreName());
            vo.setMeterModelName(pjMeter.getMsInfo().getMeterModelName());
            vo.setMeterNo(pjMeter.getMsInfo().getMeterNo());
            vo.setUseWaterTypeName(pjMeter.getUserInfo().getUseWaterTypeName());
            vo.setOpenAccountName(pjMeter.getAccountInfo().getOpenAccountName());
            vo.setBankAccount(pjMeter.getAccountInfo().getBankAccount());
            // param.put("setupMeterAddr", pjMeter.getUserInfo() == null ? "" : pjMeter.getUserInfo().getSetupMeterAddr());
            vo.setSetupMeterAddr(pjMeter.getUserInfo() == null ? "" : pjMeter.getUserInfo().getSetupMeterAddr());
            BankMapper bankMapper = factory.getMapper(BankMapper.class);
            BankBean bank = bankMapper.findBankById(pjMeter.getAccountInfo().getOpenAccountBank());
            if (bank != null) {
                vo.setOpenAccountBankName(bank.getBankName());
            } else {
                vo.setOpenAccountBankName("");
            }
            vo.setMeterBuilderName(pjMeter.getUserInfo().getMeterBuilderName());
            vo.setSetupMeterDate(pjMeter.getUserInfo().getSetupMeterDate());
            // 日期处理
            if (StringUtils.isNotBlank(Objects.toString(vo.getSetupMeterDate(), ""))) {
                LocalDate setupMeterDate = LocalDate.parse(vo.getSetupMeterDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                vo.setYear2(String.valueOf(setupMeterDate.getYear()));
                vo.setMonth2(String.valueOf(setupMeterDate.getMonthValue()));
                vo.setDay2(String.valueOf(setupMeterDate.getDayOfMonth()));
            } else {
                vo.setYear2("  ");
                vo.setMonth2("  ");
                vo.setDay2("  ");
            }
            voList.add(vo);
           // param.put("pjMeter" + index, vo);
            pjChargeMoreVOList.add(vo);

            index++;
        }
        param.put("pjChargeMoreVOList",pjChargeMoreVOList);
    }
}
